package com.ruoyi.zxjd.work.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zxjd.common.CommonConstants;
import com.ruoyi.zxjd.work.domain.WorkArrange;
import com.ruoyi.zxjd.work.domain.WorkTemplateTable1;
import com.ruoyi.zxjd.work.domain.WorkTemplateTable3;
import com.ruoyi.zxjd.work.domain.WorkTemplateTable4;
import com.ruoyi.zxjd.work.domain.WorkTemplateTable5;
import com.ruoyi.zxjd.work.service.IWorkArrangeService;
import com.ruoyi.zxjd.work.service.IWorkTemplateService;
import com.ruoyi.zxjd.work.service.IWorkTemplateTable1Service;
import com.ruoyi.zxjd.work.service.IWorkTemplateTable2Service;
import com.ruoyi.zxjd.work.service.IWorkTemplateTable3Service;
import com.ruoyi.zxjd.work.service.IWorkTemplateTable4Service;
import com.ruoyi.zxjd.work.service.IWorkTemplateTable5Service;
import com.ruoyi.zxjd.work.service.IWorkTemplateTable6Service;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工作安排Controller
 * 
 * @author lcc
 * @date 2021-01-26
 */
@Controller
@RequestMapping("/work/arrange")
public class WorkArrangeController extends BaseController
{
    private String prefix = "work/arrange";

    @Autowired
    private IWorkArrangeService workArrangeService;
    @Autowired
    private IWorkTemplateService workTemplateService;
    @Autowired
    private IWorkTemplateTable1Service workTemplateTable1Service;
    @Autowired
    private IWorkTemplateTable2Service workTemplateTable2Service;
    @Autowired
    private IWorkTemplateTable3Service workTemplateTable3Service;
    @Autowired
    private IWorkTemplateTable4Service workTemplateTable4Service;
    @Autowired
    private IWorkTemplateTable5Service workTemplateTable5Service;
    @Autowired
    private IWorkTemplateTable6Service workTemplateTable6Service;

    @RequiresPermissions("work:arrange:view")
    @GetMapping()
    public String arrange(ModelMap mmap)
    {
        mmap.put("templates", workTemplateService.selectWorkTemplateListByStatus(CommonConstants.ENABLE));
        return prefix + "/arrange";
    }

    /**
     * 查询工作安排列表
     */
    @RequiresPermissions("work:arrange:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkArrange workArrange)
    {
        startPage();
        List<WorkArrange> list = workArrangeService.selectWorkArrangeList(workArrange);
        return getDataTable(list);
    }

    /**
     * 导出工作安排列表
     */
    @RequiresPermissions("work:arrange:export")
    @Log(title = "工作安排", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkArrange workArrange)
    {
        List<WorkArrange> list = workArrangeService.selectWorkArrangeList(workArrange);
        ExcelUtil<WorkArrange> util = new ExcelUtil<WorkArrange>(WorkArrange.class);
        return util.exportExcel(list, "arrange");
    }

    /**
     * 新增工作安排
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("templates", workTemplateService.selectWorkTemplateListByStatus(CommonConstants.ENABLE));
        mmap.put("today",DateUtils.getDate());
        return prefix + "/add";
    }

    /**
     * 新增保存工作安排
     */
    @RequiresPermissions("work:arrange:add")
    @Log(title = "工作安排", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkArrange workArrange)
    {
        return toAjax(workArrangeService.insertWorkArrange(workArrange));
    }

    /**
     * 修改工作安排
     */
    @GetMapping("/edit/{arrangeId}")
    public String edit(@PathVariable("arrangeId") Long arrangeId, ModelMap mmap)
    {
        WorkArrange workArrange = workArrangeService.selectWorkArrangeById(arrangeId);
        mmap.put("workArrange", workArrange);
        mmap.put("templates", workTemplateService.selectWorkTemplateListByStatus(CommonConstants.ENABLE));
        return prefix + "/edit";
    }

    /**
     * 修改保存工作安排
     */
    @RequiresPermissions("work:arrange:edit")
    @Log(title = "工作安排", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkArrange workArrange)
    {
        return toAjax(workArrangeService.updateWorkArrange(workArrange));
    }

    /**
     * 删除工作安排
     */
    @RequiresPermissions("work:arrange:remove")
    @Log(title = "工作安排", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(workArrangeService.deleteWorkArrangeByIds(ids));
    }

    /**
     * 发布工作
     */
    @RequiresPermissions("work:arrange:release")
    @Log(title = "工作安排", businessType = BusinessType.UPDATE)
    @PostMapping("/release")
    @ResponseBody
    public AjaxResult release(Long arrangeId)
    {
        return toAjax(workArrangeService.updateStatusByArrangeId(arrangeId, CommonConstants.ONGOING));
    }

    /**
     * 撤销工作
     */
    @RequiresPermissions("work:arrange:withdraw")
    @Log(title = "工作安排", businessType = BusinessType.UPDATE)
    @PostMapping("/withdraw")
    @ResponseBody
    public AjaxResult withdraw(Long arrangeId)
    {
        return toAjax(workArrangeService.updateStatusByArrangeId(arrangeId, CommonConstants.DRAFT));
    }

    /**
     * 汇总工作
     */
    @GetMapping("/gather/{arrangeId}")
    public String gather(@PathVariable("arrangeId") Long arrangeId, ModelMap mmap)
    {
        WorkArrange workArrange = workArrangeService.selectWorkArrangeById(arrangeId);
        if(1L == workArrange.getTemplateId()){
            List<WorkTemplateTable1> WorkTemplateTable1List = workTemplateTable1Service.selectWorkTemplateTable1ByArrangeId(arrangeId);
            mmap.put("workTemplateTable1List", WorkTemplateTable1List);
            return "work/templatetable1/templatetable1";
        }
        if(2L == workArrange.getTemplateId()){
            mmap.put("workTemplateTable2", workTemplateTable2Service.selectWorkTemplateTable2ByArrangeId(arrangeId));
            return "work/templatetable2/templatetable2";
        }
        if(3L == workArrange.getTemplateId()){
            List<WorkTemplateTable3> WorkTemplateTable3List = workTemplateTable3Service.selectWorkTemplateTable3ByArrangeId(arrangeId);
            mmap.put("workTemplateTable3List", WorkTemplateTable3List);
            return "work/templatetable3/templatetable3";
        }
        if(4L == workArrange.getTemplateId()){
            List<WorkTemplateTable4> WorkTemplateTable4List = workTemplateTable4Service.selectWorkTemplateTable4ByArrangeId(arrangeId);
            mmap.put("workTemplateTable4List", WorkTemplateTable4List);
            return "work/templatetable4/templatetable4";
        }
        if(5L == workArrange.getTemplateId()){
            List<WorkTemplateTable5> WorkTemplateTable5List = workTemplateTable5Service.selectWorkTemplateTable5ByArrangeId(arrangeId);
            mmap.put("workTemplateTable5List", WorkTemplateTable5List);
            return "work/templatetable5/templatetable5";
        }
        if(6L == workArrange.getTemplateId()){
            mmap.put("workTemplateTable6", workTemplateTable6Service.selectWorkTemplateTable6ByArrangeId(arrangeId));
            return "work/templatetable6/templatetable6";
        }
        return prefix + "/gather";
    }

    @RequiresPermissions("work:arrange:view")
    @GetMapping("/allprogress/{arrangeId}")
    public String allprogress(@PathVariable("arrangeId") Long arrangeId, ModelMap mmap)
    {
        WorkArrange workArrange = workArrangeService.selectWorkArrangeById(arrangeId);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("defaultStatus", "");
        workArrange.setParams(params);
        mmap.put("workArrange", workArrange);
        return "work/progress/list";
    }

    @RequiresPermissions("work:arrange:view")
    @GetMapping("/progress/{arrangeId}")
    public String progress(@PathVariable("arrangeId") Long arrangeId, ModelMap mmap)
    {
        WorkArrange workArrange = workArrangeService.selectWorkArrangeById(arrangeId);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("defaultStatus", "4,6,7");
        workArrange.setParams(params);
        mmap.put("workArrange", workArrange);
        return "work/progress/list";
    }

    @RequiresPermissions("work:arrange:view")
    @GetMapping("/unprogress/{arrangeId}")
    public String unprogress(@PathVariable("arrangeId") Long arrangeId, ModelMap mmap)
    {
        WorkArrange workArrange = workArrangeService.selectWorkArrangeById(arrangeId);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("defaultStatus", "1,2,3,5,8");
        workArrange.setParams(params);
        mmap.put("workArrange", workArrange);
        return "work/progress/list";
    }

}
